<?php

namespace lib\session;


use entity\db\EntityCoreSession;
use model\core\SessionDb;

class DbSessionHandler implements \SessionHandlerInterface
{
    public function open($save_path, $name)
    {
        return true;
        // TODO: Implement open() method.
    }

    public function close()
    {
        // TODO: Implement close() method.
        return true;
    }

    public function destroy($session_id)
    {
        return true;
        // TODO: Implement destroy() method.
    }

    public function gc($maxlifetime)
    {
        return true;
        // TODO: Implement gc() method.
    }

    public function read($session_id)
    {
        $content = SessionDb::getBySessionId($session_id);

        return $content ?: '';
        // TODO: Implement read() method.
    }

    public function write($session_id, $session_data)
    {
        // TODO: Implement write() method.
        $entity = new EntityCoreSession;
        $entity->session_id = $session_id;
        $entity->content = $session_data;
        SessionDb::save($entity);
        return true;
    }
}